package com.cardone.validator;

import lombok.Setter;
import org.apache.commons.lang3.StringUtils;

import java.util.regex.Pattern;

/**
 * Created by Administrator on 2014/11/8.
 */
public class MatchValidatorRule extends RequiredValidatorRule {
    @Setter
    private String regex;

    @Setter
    private boolean matche = false;

    @Setter
    private boolean lowerCase = false;

    @Override
    public boolean validate(Object obj) {
        if (!(obj instanceof String)) {
            return false;
        }

        String objString = (String) obj;

        if (StringUtils.isBlank(objString)) {
            return false;
        }

        if (lowerCase) {
            objString = StringUtils.lowerCase(objString);
        }

        if (matche) {
            return Pattern.matches(regex, objString);
        }

        return !Pattern.matches(regex, objString);
    }

    public static void main(final String[] args) throws Exception {
        MatchValidatorRule matchValidatorRule = new MatchValidatorRule();

        matchValidatorRule.setLowerCase(true);

        matchValidatorRule.setRegex("^\\d{15}|\\d{18}$");

        System.out.println(matchValidatorRule.validate("select"));
    }
}
